package com.yonyougov.devops.cruxcisupport.server;

import com.yonyougov.devops.cruxcisupport.server.dto.ServerPageLikeDto;
import com.yonyougov.devops.cruxcisupport.server.vo.*;
import com.yonyougov.devops.fwc.core.dao.impl.BaseDaoImpl;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;

/**
 * t_server数据层实现
 */
@Repository
public class ServerDaoImpl extends BaseDaoImpl<ServerEntity, BigInteger> implements ServerDao {

    private final SqlSessionTemplate sqlSessionTemplate;

    public ServerDaoImpl(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    @Override
    public List<ServerPageLikeRespVoParams> findAllByParamsLike(ServerPageLikeDto serverPageLikeDto) {
        String statement = ServerEntity.class.getName() + "Mapper.findAllByParamsLike";
        return sqlSessionTemplate.selectList(statement, serverPageLikeDto);
    }

    @Override
    public int countByLikeServer(ServerPageLikeDto serverPageLikeDto) {
        String statement = ServerEntity.class.getName() + "Mapper.findAllByParamsLike-count";
        return sqlSessionTemplate.selectOne(statement, serverPageLikeDto);
    }


    @Override
    public int findNoLinkServerByUserIdCount(NoLinkServerDto noLinkServerDto) {
        String statement = ServerEntity.class.getName() + "Mapper.findNoLinkServerByUserIdCount";
        return sqlSessionTemplate.selectOne(statement, noLinkServerDto);
    }

    @Override
    public List<NoLinkServerRespVo> findNoLinkServerByUserId(NoLinkServerDto userId) {
        String statement = ServerEntity.class.getName() + "Mapper.findNoLinkServerByUserId";
        return sqlSessionTemplate.selectList(statement, userId);
    }

    @Override
    public List<NoLinkServerRespVo> findNoLinkServerByEnvId(EnvNoLinkServerPageParams pageParams) {
        String statement = ServerEntity.class.getName() + "Mapper.findNoLinkServerByEnvId";
        return sqlSessionTemplate.selectList(statement, pageParams);
    }

    @Override
    public int findNoLinkServerByEnvIdCount(EnvNoLinkServerPageParams pageParams) {
        String statement = ServerEntity.class.getName() + "Mapper.findNoLinkServerByEnvIdCount";
        return sqlSessionTemplate.selectOne(statement, pageParams);
    }

    @Override
    public void batchUpdateServerStatus(List<ServerStatusVo> statusVos) {
        String statement = ServerEntity.class.getName() + "Mapper.batchUpdateServerStatus";
        sqlSessionTemplate.update(statement, statusVos);
    }

    @Override
    public List<ServerEntity> findByIds(List<BigInteger> ids) {
        String statement = ServerEntity.class.getName() + "Mapper.findByIds";
        HashMap<Object, Object> map = new HashMap<>();
        map.put("ids", ids);
        return sqlSessionTemplate.selectList(statement, map);
    }
}
